package com.hncj.yqfk.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 将两个list集合混合为一个list集合，相同时间合并，不同时间排序
 */
public class MixInfo {
    public static LinkedHashMap<String, Object> mixtwoinfo(List<Map<String, Object>> listmap1, List<Map<String, Object>> listmap2) {
        //创建时间和人流数量
        ArrayList<String> timelist = new ArrayList<String>();
        ArrayList<Integer> numlist = new ArrayList<Integer>();
        //对listmap1集合进行数据提取
        for (Map map : listmap1) {
            timelist.add(map.get("HOUR").toString());
            numlist.add(Integer.parseInt(map.get("HOUR_JNUM").toString()));
        }
        //对listmap2的数据进行提取，追加到两个list集合中
        for (Map map : listmap2) {
            String time = map.get("HOUR").toString();
            if (timelist.contains(time)) {
                Integer index = timelist.indexOf(time);
                numlist.set(index, numlist.get(index) + Integer.parseInt(map.get("HOUR_JNUM").toString()));
            } else {
                timelist.add(time);
                numlist.add(Integer.parseInt(map.get("HOUR_JNUM").toString()));
            }
        }
        //根据时间先后进行排序操作
        for (int i = 0; i < timelist.size()-1; i++){
            for (int j = 1; j < timelist.size() - i; j++) {
                if (!TimeUtils.Comparetime(timelist.get(j-1),timelist.get(j))){
                    //设置排序后的时间
                    String temptime = timelist.get(j-1);
                    timelist.set((j-1),timelist.get(j));
                    timelist.set(j,temptime);
                    //设置排序后的人流数
                    Integer tempnum = numlist.get(j-1);
                    numlist.set((j-1),numlist.get(j));
                    numlist.set(j,tempnum);
                }
            }
        }
        // 创建返回集合
        LinkedHashMap<String,Object> map = new LinkedHashMap<>();
        for (int i = 0; i < timelist.size(); i++) {
            map.put(timelist.get(i), numlist.get(i));
        }
        return map;
    }
}
